package com.sinodata.bsm.cicp.collector.sybase;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * Device.java
 * <p>
 *
 * @author 管普贵 guanpg@uni-mon.com
 * @version 2.0 
 * @Date Jan 8, 2010
 */
public class Device extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(Device.class);

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            String serverName = this.getParamMap().get("ServerName");
            String devName = this.getParamMap().get("DeviceName");
            // String sql = "select sd.name, (select sum(u.size)*(@@pagesize/1048576.0) from master..sysusages u" + " where u.vstart>=sd.low and u.vstart<=sd.high) used,(select ltrim(str((d.high - d.low)*(@@pagesize/1048576.0), 10, 0))" + " from master..sysdevices d  where d.name = sd.name) total from master..sysdevices sd " + " where sd.cntrltype = 0 and (sd.status & 2) = 2 and sd.name='" + devName + "'";
            // isnull(sum(u.size)*(@@pagesize/1048576.0)*2,0)  where u.vdevno=sd.vdevno  by liulb 2011-8-11
            String sql = "select sd.name, (select isnull(sum(u.size)*(@@pagesize/1048576.0)*2,0) from master..sysusages u" + " where u.vdevno=sd.vdevno) used,(select ltrim(str((d.high - d.low)*(@@pagesize/1048576.0), 10, 0))" + " from master..sysdevices d  where d.name = sd.name) total from master..sysdevices sd " + " where sd.cntrltype = 0 and (sd.status & 2) = 2 and sd.name='" + devName + "'";
            String[] rs = this.getStrings(serverName, sql, ip);
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sybase device", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[2];
        try {
            ResultSet rst = this.getCachedResultSet("sybase", ip, serverName, sql);
            float used = 0.0F;
            float total = 0.0F;
            float freeSize = 0.0F;
            while (rst.next()) {
                used = rst.getFloat("used");
                total = rst.getFloat("total");
                freeSize = total - used;
            }
            propValArr[0] = total + "";
            propValArr[1] = freeSize + "";
        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }
        return propValArr;
    }

    public String[] getParamMeta() {
        return new String[] { "ServerName", "DeviceName" };
    }

    public String[] getPropMeta() {
        return new String[] { "TotalSpaceSize", "FreeSpaceSize" };
    }
}
